package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.WordbookMapper;
import com.yxh.exception.UnitException;
import com.yxh.exception.WordbookException;
import com.yxh.pojo.Wordbook;
import com.yxh.pojo.tree.BaseTreeGrid;
import com.yxh.service.WordbookService;
import com.yxh.util.PropertiesUtil;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import com.yxh.util.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.yxh.util.APIErrorCode.*;

/**
 * Created by 32988 on 2018/6/2.
 */
@Service("wordbookService")
@Transactional
public class WordbookServiceImpl implements WordbookService {

    @Autowired
    WordbookMapper wordbookMapper;

    @Override
    public PageInfo getInformation(Wordbook wordbook, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Wordbook> wordbookList = wordbookMapper.selectByWordbook(wordbook);
        PageInfo pageInfo = new PageInfo(wordbookList);
        pageInfo.setList(wordbookList);
        return pageInfo;
    }

    @Override
    public  List<Wordbook> getFault() {
        List<Wordbook> wordbooks =  wordbookMapper.selectByFineTypeId(PropertiesUtil.getProperty("fault.fineTypeId"));
        return wordbooks;
    }

    @Override
    public List<Wordbook> getFineType(String fineTypeId) {
        List<Wordbook> wordbooks =  wordbookMapper.selectByFineTypeId(PropertiesUtil.getProperty(fineTypeId));
        return wordbooks;
    }

    @Override
    public List<BaseTreeGrid> getFineTypeTree(String value) {

        List<BaseTreeGrid> list=wordbookMapper.selectlist(value);
        TreeUtil tree = new TreeUtil();
        List<BaseTreeGrid> treeGrid = tree.transTypeTree(list);
        return treeGrid;
    }

    @Override
    public int save(Wordbook record) {
        record.setWordbookId(SnowflakeIdWorker.creatId());
        int count = wordbookMapper.insert(record);
        if (count == 0) {
            throw new WordbookException(INTERNAL_ERROR);
        }
        return count;
    }

    @Override
    public int update(Wordbook record) {
        int count = wordbookMapper.updateByPrimaryKeySelective(record);
        if (count == 0) {
            throw new WordbookException(PARAMS_INVALID);
        }
        return count;
    }

    @Override
    public int delete(String id) {
        if(!StringUtil.isEmpty(id)){
            String [] arr = id.split(",");
            int count = wordbookMapper.deleteByIds(arr);
            if (count == 0) {
                throw new WordbookException(PARAMS_INVALID);
            }
        }
        return 0;
    }

    @Override
    public void batchDelete(List<Wordbook> records) {

    }

    @Override
    public Wordbook get(String id) {
        return null;
    }

    @Override
    public Wordbook get(Wordbook record) {
        return null;
    }

    @Override
    public List<Wordbook> getList(Wordbook record) {
        return null;
    }

    @Override
    public List<Wordbook> getAllList(Wordbook record) {
        return null;
    }

    @Override
    public List<Wordbook> getListPage(PageEntity<Wordbook> record) {
        return null;
    }
}
